<template>
<!-- pages/selectAddress/selectAddress.wxml -->
<view class="selectAddress">
    <view class="address_list">
        <view v-for="(item, index) in address_list" :key="index" class="address_item" :data-id="item.id" @tap="onChangeAddress">
            <view class="item_top">
                <view class="item_info">
                    <text class="info_name">{{item.nickname}}</text>
                    <text class="info_phone">{{item.phone}}</text>
                </view>
                <text class="item_txt">{{item.area}}</text>
            </view>
            <view class="item_bottom">
                <view class="bottom_left" @click.stop="onChangeDefault(item.id,item.is_default)">
                    <image v-if="item.is_default == 1" class="left_img" src="/static/pages/image/chcked.png"></image>
                    <view v-else class="left_round"></view>
                    <text class="left_txt">默认地址</text>
                </view>
                <view class="bottom_right">
                    <text class="del"  @click.stop="onDel(item.id)">删除</text>
                    <view class="line"></view>
                    <text class="compile" @click.stop="onEdit(item.id)">编辑</text>
                </view>
            </view>
        </view>
    </view>

    <view class="footer">
        <button class="footer_buttom" @tap="toAddAddress">添加新地址</button>
    </view>
</view>
</template>

<script>
// pages/selectAddress/selectAddress.js
var app = getApp();

export default {
  data() {
    return {
      currentSelectId: null,
      address_list: []
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAddressList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  methods: {
    getAddressList() {
      wx.showLoading({
        title: '加载中'
      });
      app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          m: "monai_market",
          r: "member.index.receiptlist",
          uid: wx.getStorageSync('uid')
        },
        success: res => {
          this.setData({
            address_list: res.data.data
          });
          wx.hideLoading();
        },
        fail: res => {
          wx.hideLoading();
        }
      });
    },

    toAddAddress() {
      wx.navigateTo({
        url: '/pagesA/address/address'
      });
    },

    onChangeDefault(id,is_default) {
      if (is_default == 1) return;
      wx.showLoading({
        title: '加载中'
      });
      app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          m: "monai_market",
          r: "member.index.receipt_is_default",
          uid: wx.getStorageSync('uid'),
          is_default: 1,
          id: id
        },
        success: res => {
          this.getAddressList();
          wx.hideLoading();
        },
        fail: res => {
          wx.hideLoading();
        }
      });
    },

    // 删除
    onDel(id) {
      wx.showLoading({
        title: '加载中'
      });
      app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          m: "monai_market",
          r: "member.index.receipt_del",
          uid: wx.getStorageSync('uid'),
          id: id
        },
        success: res => {
          this.getAddressList();
          wx.hideLoading();
        },
        fail: res => {
          wx.hideLoading();
        }
      });
    },

    onEdit(id) {
		uni.navigateTo({
			url:'/pagesA/address/address?id=' + id
		})
    },

    // 改变地址
    onChangeAddress(event) {
      const id = event.currentTarget.dataset['id'];
      wx.setStorageSync('currentSelectId', id);
      wx.navigateBack();
    }

  }
};
</script>
<style>
@import "./selectAddress.css";
</style>